package com.zhy.sping.service.impl;

import com.zhy.sping.config.RedisKeyConfig;
import com.zhy.sping.dao.CouponDao;
import com.zhy.sping.dao.UserCouponDao;
import com.zhy.sping.dto.CouponOpDto;
import com.zhy.sping.entity.Coupon;
import com.zhy.sping.entity.UserCoupon;
import com.zhy.sping.service.intf.CouponService;
import com.zhy.sping.util.RedissionUtil;
import com.zhy.sping.vo.R;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponDao couponDao;
    @Autowired
    private UserCouponDao userCouponDao;
    @Override
    @Transactional //开启事务注解
    public R add(int uid, int cid) {
        //验证用户是否领取过优惠券
    if (userCouponDao.select(uid, cid)==null){
        RLock lock = RedissionUtil.lock(RedisKeyConfig.KEY_LOCK_COUPON + cid);
        try {
            if (lock.tryLock(RedisKeyConfig.KEY_LOCK_COUPON_TIME, TimeUnit.SECONDS)){
                //验证库存
                boolean isFirst=true,isttl=false;
                //验证缓存优惠券的集合存在
                if (RedissionUtil.exists(RedisKeyConfig.KEY_COUPON_CACHE)){
                    if (RedissionUtil.checkHashFeild(RedisKeyConfig.KEY_COUPON_CACHE,cid+"")){
                        isFirst=false;
                        int ct = Integer.parseInt(RedissionUtil.getHash(RedisKeyConfig.KEY_COUPON_CACHE, cid + ""));
                        if (ct >0){
                            //可以领取
                            if (userCouponDao.insert(new UserCoupon()) >0){
                                //更改库存
                                RedissionUtil.addHash(RedisKeyConfig.KEY_COUPON_CACHE,cid+"",(ct-1)+"");
                                return R.ok("ok",null);
                            }
                        }
                        return R.fail("亲，库存不够了");
                    }
                }else {
                    isttl=true;
                }

                if (isFirst){
                    Coupon coupon = couponDao.selectById(cid);
                    //可以领取
                    if (coupon.getStock()>0){
                        if (userCouponDao.insert(new UserCoupon()) >0){
                            if (coupon.getStock() ==1){
                                //库存为0
                                couponDao.update(cid,0);
                            }else {
                                RedissionUtil.addHash(RedisKeyConfig.KEY_COUPON_CACHE,cid+"",(coupon.getStock()-1)+"");
                                if (isttl){
                                    RedissionUtil.expire(RedisKeyConfig.KEY_COUPON_CACHE,RedisKeyConfig.KEY_COUPON_CACHE_TIME);

                                }
                            }
                                return R.ok("ok",null);

                        }
                    }
                }


            }else {
                return R.fail("亲，网络故障");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            RedissionUtil.unlock(RedisKeyConfig.KEY_LOCK_COUPON+cid);
        }
    }else {
        return R.fail("亲，您已领取过优惠券");
    }
        return R.fail("亲,网络故障，请稍后再试");
    }

    @Override
    public R consumption(CouponOpDto dto) {
        return null;
    }

    @Override
    public R couponcancel(CouponOpDto dto) {
        return null;
    }
}
